<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page isELIgnored="true" %>

<!-- 个人中心 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>个人中心</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <!-- 样式 -->
    <link rel="stylesheet" href="../../css/style.css"/>
    <!-- 主题（主要颜色设置） -->
    <link rel="stylesheet" href="../../css/theme.css"/>
    <!-- 通用的css -->
    <link rel="stylesheet" href="../../css/common.css"/>
</head>
<body style="background: #EEEEEE;">

<div id="app">
    <!-- 轮播图 -->
    <div class="layui-carousel" id="swiper">
        <div carousel-item id="swiper-item">
            <div v-for="(item,index) in swiperList" v-bind:key="index">
                <img class="swiper-item" :src="item.img">
            </div>
        </div>
    </div>
    <!-- 轮播图 -->

    <!-- 标题 -->
    <h2 style="margin-top: 20px;" class="index-title">USER / CENTER</h2>
    <div class="line-container">
        <p class="line" style="background: #EEEEEE;"> 个人中心 </p>
    </div>
    <!-- 标题 -->

    <div class="center-container">
        <!-- 个人中心菜单 config.js-->
        <div class="left-container">
            <ul class="layui-nav layui-nav-tree">
                <li v-for="(item,index) in centerMenu" v-bind:key="index" class="layui-nav-item"
                    :class="index==0?'layui-this':''">
                    <a :href="'javascript:jump(\''+item.url+'\')'">{{item.name}}</a>
                </li>
            </ul>
        </div>
        <!-- 个人中心菜单 -->
        <!-- 个人中心 -->
        <div class="right-container">
            <form class="layui-form" lay-filter="myForm">
                <!-- 主键 -->
                <input type="hidden" name="id" id="id"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" placeholder="用户名" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
               
                <div class="layui-form-item">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" id="username" placeholder="账号" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="text" name="password" id="password" placeholder="密码" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <img id="imgPhotoImg"
                             style="width: 100px;height: 100px;border-radius: 50%;border: 2px solid #EEEEEE;"
                             src="../../img/avator.png">
                        <input type="hidden" id="imgPhoto" name="imgPhoto"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn btn-theme" id="imgPhotoUpload">
                            <i class="layui-icon">&#xe67c;</i>上传图片
                        </button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">手机</label>
                    <div class="layui-input-block">
                        <input type="text" name="phone" id="phone" placeholder="手机" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn btn-submit btn-theme" lay-submit lay-filter="*">更新信息</button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button @click="logout" class="layui-btn btn-submit">退出登录</button>
                    </div>
                </div>
            </form>
        </div>
        <!-- 个人中心 -->
    </div>

</div>

<!-- layui -->
<script src="../../layui/layui.js"></script>
<!-- vue -->
<script src="../../js/vue.js"></script>
<!-- 组件配置信息 -->
<script src="../../js/config.js"></script>
<!-- 扩展插件配置信息 -->
<script src="../../modules/config.js"></script>
<!-- 工具方法 -->
<script src="../../js/utils.js"></script>
<!-- 校验格式工具类 -->
<script src="../../js/validate.js"></script>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            // 轮播图
            swiperList: [{
                img: '../../img/banner.jpg'
            }],
            centerMenu: centerMenu
        },
        updated: function () {
            layui.form.render(null, 'myForm');
        },
        methods: {
            jump(url) {
                jump(url)
            },
            logout() {
                localStorage.removeItem('Token');
                localStorage.removeItem('role');
                localStorage.removeItem('sessionTable');
                localStorage.removeItem('adminName');
                localStorage.removeItem('userid');
                localStorage.removeItem('userTable');
                window.parent.location.href = '../login/login.jsp';
            }
        }
    })

    layui.use(['layer', 'element', 'carousel', 'http', 'jquery', 'form', 'upload'], function () {
        var layer = layui.layer;
        var element = layui.element;
        var carousel = layui.carousel;
        var http = layui.http;
        var jquery = layui.jquery;
        var form = layui.form;
        var upload = layui.upload;

        // 充值
        jquery('#btn-recharge').click(function (e) {
            layer.open({
                type: 2,
                title: '用户充值',
                area: ['900px', '600px'],
                content: '../recharge/recharge.jsp'
            });
        });

        // 获取轮播图 数据
        http.request('config/list', 'get', {
            page: 1,
            limit: 5
        }, function (res) {
            if (res.data.list.length > 0) {
                var swiperItemHtml = '';
                for (let item of res.data.list) {
                    if (item.name.indexOf('picture') >= 0 && item.value && item.value != "" && item.value != null) {
                        swiperItemHtml +=
                            '<div>' +
                            '<img class="swiper-item" src="' + item.value + '">' +
                            '</div>';
                    }
                }
                jquery('#swiper-item').html(swiperItemHtml);
                // 轮播图
                carousel.render({
                    elem: '#swiper',
                    width: swiper.width, height: swiper.height,
                    arrow: swiper.arrow,
                    anim: swiper.anim,
                    interval: swiper.interval,
                    indicator: "none"
                });
            }
        });


        // 上传图片
        var imgPhotoUpload = upload.render({
            //绑定元素
            elem: '#imgPhotoUpload',
            //上传接口
            url: http.baseurl + 'file/upload',
            // 请求头
            headers: {
                Token: localStorage.getItem('Token')
            },
            // 允许上传时校验的文件类型
            accept: 'images',
            before: function () {
                //loading层
                var index = layer.load(1, {
                    shade: [0.1, '#fff'] //0.1透明度的白色背景
                });
            },
            // 上传成功
            done: function (res) {
                console.log(res);
                layer.closeAll();
                if (res.code == 0) {
                    layer.msg("上传成功", {
                        time: 2000,
                        icon: 6
                    })
                    var url = http.baseurl + 'upload/' + res.file;
                    jquery('#imgPhoto').val(url);
                    jquery('#imgPhotoImg').attr('src', url)
                } else {
                    layer.msg(res.msg, {
                        time: 2000,
                        icon: 5
                    })
                }
            },
            //请求异常回调
            error: function () {
                layer.closeAll();
                layer.msg("请求接口异常", {
                    time: 2000,
                    icon: 5
                })
            }
        });

        // 查询用户信息
        let table = localStorage.getItem("userTable");

        if (!table) {
            layer.msg('请先登录', {
                time: 2000,
                icon: 5
            }, function () {
                window.parent.location.href = '../login/login.jsp';
            });
        }


        http.request(`${table}/session`, 'get', {}, function (data) {
            form.val("myForm", data.data);
            // 图片赋值
            jquery("#imgPhotoImg").attr("src", data.data.imgPhoto);
        });




        // 提交表单
        form.on('submit(*)', function (data) {
            data = data.field;
            http.requestJson(table + '/update', 'post', data, function (res) {
                layer.msg('修改成功', {
                    time: 2000,
                    icon: 6
                }, function () {
                    window.location.reload();
                });
            });
            return false
        });

    });
</script>
</body>
</html>
